﻿using System;
using System.Text;

namespace WpfClient.FunctionFileParsing
{
    public class FunctionFileParseResult
    {
        private const string FunctionSummaryFormatString = @"    /// <summary>
    /// Represents the {0} function.
    /// </summary>
";

        public string FunctionName { get; set; }
        public int ClassStartIndex { get; set; }
        public string ParsedText { get; set; }

        public string GetResultFileContent()
        {
            StringBuilder result = new StringBuilder();            

            int splitIndex = this.SplitIndex;

            string start = ParsedText.Substring(0, splitIndex);
            string end = ParsedText.Substring(splitIndex, ParsedText.Length - splitIndex);
            string middle = this.FunctionSummary;

            result.AppendFormat("{0}{1}{2}", start, middle, end);

            return result.ToString();
        }

        private int SplitIndex
        {
            get
            {
                int index = this.ClassStartIndex;
                while (this.ParsedText[index] != '\n' && this.ParsedText[index] != '\r' && index > 0)
                {
                    index--;
                }

                return index + 1;
            }
        }

        private string FunctionSummary
        {
            get
            {
                return String.Format(FunctionSummaryFormatString, this.FunctionName);
            }
        }
    }
}
